package dfbz.com.service.impl;

import dfbz.com.dao.UserDao;
import dfbz.com.dao.UserFocusDao;
import dfbz.com.dao.UserInfoDao;
import dfbz.com.model.User;
import dfbz.com.model.UserFocus;
import dfbz.com.model.UserInfo;
import dfbz.com.service.UserService;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDao();

    private UserInfoDao userInfoDao = new UserInfoDao();

    private UserFocusDao userFocusDao = new UserFocusDao();


    @Override
    public User findUserByName(String username) {
        return userDao.findUserByName(username);
    }

    @Override
    public User findUserByEmail(String email) {
        return userDao.findUserByEmail(email);
    }

    @Override
    public Map<String, Object> findUserBaseById(Integer userId) {
        return userDao.findUserById(userId);
    }

    @Override
    public void addUser(String email, String username, String password) {
        User user = new User();
        user.setId(null);//保险一点
        user.setEmail(email);
        user.setUsername(username);
        user.setPassword(password);
        user.setIsSecret("0");
        userDao.add(user);

        User dbUser = userDao.findUserByName(username);
        //下一步还要给当前用户初始化用户信息 登录时间，创建时间
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(dbUser.getId());
        Date now = new Date();
        userInfo.setRegisterTime(now);
        userInfo.setLoginTime(now);
        userInfoDao.add(userInfo);

    }

    @Override
    public void updateLoginTime(Integer id) {
        //通过ID查到用户
        User user = userDao.get(id, User.class);
        //更新用户信息
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(user.getId());
        userInfo.setLoginTime(new Date());
        userInfoDao.update(userInfo);

    }

    @Override
    public void resetPass(Integer userId, String password) {
        User user = new User();
        user.setId(userId);
        user.setPassword(password);
        userDao.update(user);
    }

    @Override
    public List<Map<String, Object>> findList(Integer userId, int page, String search) {
        List<Map<String, Object>> list = userDao.findList(userId, page, search);
        for (Map<String, Object> map : list) {
            if (null == map.get("realName")) {
                map.put("realName", map.get("username"));
            }
        }
        return list;
    }

    @Override
    public long findTotalUserCount() {
        return userDao.getTotalUser();
    }

    @Override
    public void unFollow(Integer ufId) {
        userFocusDao.del(ufId, UserFocus.class);
    }

    @Override
    public UserFocus findFollowData(Integer userId, Integer userFocusId) {
        return userFocusDao.findFollowData(userId, userFocusId);
    }

    @Override
    public void followUser(Integer userId, Integer userFocusId) {
        UserFocus focus = new UserFocus();
        focus.setId(null);
        focus.setUserId(userId);
        focus.setUserFocusId(userFocusId);
        userFocusDao.add(focus);
    }


}
